﻿/// <reference path="../../JQ/referencejs.js" />

$(document).ready(function () {
    //为所有元素增加不可以选中的属性
    $('*').attr('unselectable', 'on');
    var designer = new TableDesigner();
});

//取消右键事件
$(document).contextmenu({ items: [{
    text: '刷新页面',
    action: function () {
        window.location.reload();
    }
}], width: '150px'
});

var TableDesigner = function () {
    //表单设计器的对应dll
    this.assemblyName = "DreamCube.Websites.TableDesigner,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null";
    this.designerZone = null; //设计区域
    this.propertyWin = null;
    this.init();
};

TableDesigner.prototype.init = function () {
    /// <summary>表单设计器初始化</summary>
    this.controlPanelInit(); //控件区域初始化
    this.designerZoneInit(); //设计区域初始化
    this.toolbarInit();
    this.propertyWinInit();
};

TableDesigner.prototype.propertyWinInit = function () {
    /// <summary>属性窗体初始化</summary>
    this.propertyWin = new PropertyWin({ itemTitleCSS: { 'width': '2cm' }, itemValCSS: { 'width': '4cm'} });
    //{ titleCSS: { 'display': 'none'} }
    this.propertyWin.appendTo("div_propertyWin");
};

TableDesigner.prototype.designerZoneInit = function () {
    /// <summary>设计区域初始化</summary>
    JQDD.setDropEl("designZone");
    this.designerZone = new DesignerZone("designZone");
    //自定义事件
    $(this.designerZone).bind('focusElChange', { TableDesigner: this }, function (event, focusEl) {
        /// <summary>改变选中的元素触发的事件</summary>
        var focusObj = $(focusEl);
        if (focusObj.length === 1) {
            var ctype = focusObj[0].ctype;
            window.status = ctype;
            var property = ControlProperty.getProperty(ctype);
            event.data.TableDesigner.propertyWin.updateData(property);
        }
    });
};

TableDesigner.prototype.toolbarInit = function () {
    /// <summary>工具栏初始化</summary>
    var me = this;
    $(".btns").mouseover(function () {
        $(this).addClass("btnHover");
    }).mouseleave(function () {
        $(this).removeClass("btnHover");
    }).mousedown(function () {
        //单击下的时候显示别的样式
        $(this).removeClass("btnHover");
        $(this).addClass("btnHoverFocus");
        var me = this;
        $(".btns").removeClass(function () {
            if (this != me) return "btnHoverFocus";
            return false;
        });
    });

    //预览按钮事件
    $("#preview").click(function (e) {
        me.preview();
    });

    //保存按钮的事件
    $("#save").click(function (e) {
        me.save();
    });
};

TableDesigner.prototype.controlPanelInit = function () {
    /// <summary>控件区域初始化</summary>
    $(".control").mouseover(function () {
        $(this).addClass("controlHover");
    }).mouseleave(function () {
        $(this).removeClass("controlHover");
    }).mousedown(function () {
        //单击下的时候显示别的样式
        $(this).removeClass("controlHover");
        $(this).addClass("controlFocus");
        var me = this;
        $(".control").removeClass(function () {
            if (this != me) return "controlFocus";
            return false;
        });
    });
    var me = this;
    $(".control").each(function () {
        JQDD.setDrapEl(this, function (e, dropTarget) {
            //投放前触发的事件
            //释放鼠标的时候，要首先要获取当前的位置
            var drapEl = this; //当前拖住元素
            if (dropTarget) {
                var ctype = $(this)[0].ctype;
                if (ctype) {
                    var bResult = me.designerZone.createControl(ctype, e.pageX, e.pageY);
                    //比较字符串
                    if (bResult.toString() == "true") {
                        var property = ControlProperty.getProperty(ctype);
                        me.propertyWin.updateData(property);
                    }
                }
            }
        }, function () {
            //投放后的事件
        });
    });
};

//执行预览
TableDesigner.prototype.preview = function () {
    var sHtml = this.designerZone.getDesignData();
    sHtml = $.htmlEncode(sHtml);
    $("#DesignerTable_html").val(sHtml);
    var oForm = document.forms["postForm"];
    oForm.action = "Preview.aspx";
    oForm.submit();
};

//执行保存操作
TableDesigner.prototype.save = function () {
    //var sHtml = this.designerZone.getDesignData();
    //sHtml = $.htmlEncode(sHtml);
    //var sFunName = "DreamCube.Websites.TableDesigner.DesignerTable.Create";
    ////String name, String pageUrl, String html
    //var parameters = ['', '', sHtml];
    //WaitSchedulebar.show();
    //MyRemoteCall.execServerFunctionAsync(sFunName, sAssemblyName, parameters, '', function (bReturn) {
    //    WaitSchedulebar.hide();
    //});

    //var sHtml = this.designerZone.getDesignData();
    //sHtml = $.htmlEncode(sHtml);
    //$("#DesignerTable_html").val(sHtml);
    ////序列化成字符串
    //var sValue = $("#postForm").serialize();
    //var sUrl = MyWebsite.getServerAddr() + "Common/Agents/FormSave.ashx";
    //MyAjax.post(sUrl, sValue, true);
    var oForm = new JQForm("postForm", { events: { saved: function (sPostBack) {
        alert(sPostBack);
    }
    }
    });
    oForm.save();
};